package com.oocl.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.pojo.Address;
import com.oocl.service.AddressManager;
import com.oocl.service.factory.AddressManagerFactory;

/**
 * Servlet implementation class AddressSearchServlet
 */
public class AddressSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private AddressManager manager;
	
    public AddressSearchServlet() {
        super();
        manager = AddressManagerFactory.getInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		String cname = request.getParameter("cityName");
		List<Address> addrs= null;
		if(null==cname || "".equals(cname.trim())){
			addrs = new ArrayList<>();
		}else {
			addrs = manager.findAddressByNameLike(cname);
		}
		ObjectMapper mapper = new ObjectMapper();
		OutputStream outputStream = response.getOutputStream();
		mapper.writeValue(outputStream, addrs);
		outputStream.close();
	}

}
